<?php

namespace App\Models\Api;

use Log;

class ApiResponse
{
    /**
     * @param $code
     * @param $msg
     * @param $data
     * @return array|mixed
     */
    public static function apiResponse($code, $msg, $data = [])
    {
        if (is_object($data)) {
            $data = json_decode(json_encode($data), true);
        }

        if ($data && array_key_exists('per_page', $data)) {//分页数据
            $data['code'] = $code;
            $data['msg'] = $msg;
//            Log::debug($data);
            return $data;
        }
//        Log::debug(['code' => $code, 'msg' => $msg, 'data' => $data]);
        return ['code' => $code, 'msg' => $msg, 'data' => $data];//普通数据
    }

    //返回成功信息
    public static function success($message, $data = [])
    {
        if (is_string($message)) {
            return self::apiResponse('1', $message, $data);
        }
        return self::apiResponse('1', '', $message);
    }

    //返回错误信息
    public static function fail($message, $data = [])
    {
        if (is_string($message)) {
            return self::apiResponse('0', $message, $data);
        }
        return self::apiResponse('0', '', $message);
    }

    //未登陆
    public static function nlogin()
    {
        return self::apiResponse('-1', '用户未登录,请登录', []);
    }
}
